﻿//	Map_FVG C# Class Library.
//
//	infoDialog5
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;


namespace Map_Fvg
{
    public partial class infoDialog5 : Form
    {
        public infoDialog5()
        {
            InitializeComponent();

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    string ss = "select * from classi INNER JOIN linea ON classi.codice_classe = linea.codice_classe WHERE linea.codice_stclasse = '";
                    ss = string.Concat(ss, Simbol.selectedLinea, "'");

                    cmd.CommandText = ss;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            textBox6.Text = reader.GetString(1).ToString();
                            textBox1.Text = reader.GetString(2).ToString();
                            textBox4.Text = reader.GetString(4).ToString();
                            textBox5.Text = reader.GetString(5).ToString();
                            textBox2.Text = reader.GetString(6).ToString();
                            textBox3.Text = reader.GetString(7).ToString();
                        }
                    }
                }

                conn.Close();
            }

            string[] words;
            char[] delimiterChars = { ' ', ',' };

            string s1 = textBox3.Text;
            textBox3.Text = "";
            words = s1.Split(delimiterChars);
            byte b1 = byte.Parse(words[0]);
            byte b2 = byte.Parse(words[1]);
            byte b3 = byte.Parse(words[2]);
            byte b4 = byte.Parse(words[3]);
            textBox3.BackColor = Color.FromArgb(b1, b2, b3, b4);
 
        }
 
        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //  conferma

            string s5 = textBox4.Text;
            if (s5.Length == 0)
            {
                MessageBox.Show("manca denominazione linea");
                return;
            }
            string s6 = textBox5.Text;
            if (s6.Length == 0)
            {
                MessageBox.Show("indicare tipo linea");
                return;
            }
            s6 = s6.Trim();

            string s3 = textBox2.Text;
            if (s3.Length == 0)
            {
                MessageBox.Show("manca descrizione tratto");
                return;
            }
            s3= s3.Trim();

            Color icol = textBox3.BackColor;

            string ss = icol.A.ToString();

            ss = string.Concat(ss," ", icol.R.ToString()," ", icol.G.ToString()," ", icol.B.ToString());

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    ss = "update linea set denom_stclasse = '" + s5 + "', tipo = '" + s6 + "', tratto = '" + s3 + "', colore ='" + ss + "' WHERE codice_stclasse = '";
                    ss = string.Concat(ss, Simbol.selectedLinea, "'");

                    cmd.CommandText = ss;

                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            this.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //  annulla

            this.Close();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog();
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox3.BackColor = colorDialog1.Color;
            }
        }
    }
}
